package com.lc.afbx.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.activity.NewsDetailsActivity;
import com.lc.afbx.adapters.NewsAdapter;
import com.lc.afbx.conn.GetApiInformationIndex;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/10/21.
 */
public class NewsCompanyFragment extends AppV4Fragment {

    private PullToRefreshListView pullToRefreshListView;
    private ListView fragment_news_lv;

    private NewsAdapter adapter;

    private List<GetApiInformationIndex.DataZixun> lists = new ArrayList<>();

    private GetApiInformationIndex getApiInformationIndex = new GetApiInformationIndex(new AsyCallBack<GetApiInformationIndex.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiInformationIndex.Info info) throws Exception {

            lists.addAll(info.dataList);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(getActivity(), toast);

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.onPullUpRefreshComplete();
            pullToRefreshListView.onPullDownRefreshComplete();

            setLastRefreshTime();

        }
    });


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View view = BaseApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_news, null));

        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.fragment_news_lv);

        pullToRefreshListView.setPullLoadEnabled(false);
        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lists.clear();
                getApiInformationIndex.tid = "1";
                getApiInformationIndex.execute(getActivity(), false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        fragment_news_lv = pullToRefreshListView.getRefreshableView();

        fragment_news_lv.setDivider(null);

        fragment_news_lv.setAdapter(adapter = new NewsAdapter(getActivity(), lists));

        fragment_news_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), NewsDetailsActivity.class);

                intent.putExtra("id", lists.get(position).id);
                intent.putExtra("title", lists.get(position).title);

                startActivity(intent);
            }
        });

        getApiInformationIndex.tid = "1";
        getApiInformationIndex.execute(getActivity());

        return view;
    }

    public void setLastRefreshTime() {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("HH:mm:ss");

        pullToRefreshListView.setLastUpdatedLabel(sDateFormat.format(new Date()));

    }


}
